<?php
namespace app\designer\controller;

use app\admin\library\AdminController;
use cubox\Data;
class Index extends AdminController{

    public function __construct()
    {
        parent::__construct();
        $this->power('designer');
    }

    public function onIndex(){
        return $this->display();
    }

    public function onTest(){
        return $this->displayBlank();
    }

    public function onList(){
        $appPath = ROOT.DS.APP;

        $noIcon = $this->assetUrl(ROOT.DS.APP.DS.'designer'.DS.'assets'.DS.'image'.DS.'app.png');
        $list = [];
        if (is_dir($appPath)){
            $dirlist = scandir($appPath);

            
            foreach($dirlist as $key => $value){
                if ($value != '.' && $value != '..'){
                    $projectPath =$appPath.DS.$value.DS.'project.php';

                    if ($this->app->design($value) && file_exists($projectPath)){
                        
                        $project = new Data($projectPath);
                        $project->read();

                        $list[] = [
                            'name'=>$value,
                            'title' => $project->title,
                            'version' => $project->version,
                            'author' => $project->author,
                            'description' => $project->description,
                            'keywords' => $project->keywords,
                            'copyright' => $project->copyright,
                            'license' => $project->license,
                            'icon' =>$project->icon==''?$noIcon:$this->assetUrl($appPath.DS.$value.DS.$project->icon),
                        ];
                       
                    }

                }
            }
        }

        return $list;
    }

    public function onEdit(){
        
        $name = $this->get('name',$this->check()->type('stirng')->required());

        $this->pageTitle = '编辑应用';

        $projectPath =ROOT.DS.APP.DS.$name.DS.'project.php';

        if (!file_exists($projectPath)){
            throw new \Exception('应用不存在');
        }
        
        if (!$this->app->design($name)){
            throw new \Exception('该应用不支持设计');
        }

        $projectPath = ROOT.DS.APP.DS.$name.DS.'project.php';

        if (file_exists($projectPath)){
            $project = new Data($projectPath);
            $project->read();

            $this->set('project',$project->toArray());
        }

        $designerPath =ROOT.DS.APP.DS.$name.DS.'_setup'.DS.'config.php';
        if (file_exists($designerPath)){
            $designer = new Data($designerPath);
            $designer->read();
            $this->set('designer',$designer->toArray());
        }

        return $this->display();
    }


    public function onCreate(){
        
        $name = $this->post('name',$this->check('名称')->type('string')->required()->reg('/^[A-Za-z]{1}[A-Za-z0-9_]{2,49}$/'));
        $title = $this->post('title',$this->check('标题')->type('string')->required());
        $type = $this->post('type',$this->check('类型')->type('string')->required());
        $appPath = ROOT.DS.APP.DS.$name;

        if(is_dir($appPath)){
            throw new \Exception('应用已存在');
        }

        if(!mkdir($appPath)){
            throw new \Exception('创建应用失败');
        }


        $projectPath =ROOT.DS.APP.DS.$name.DS.'project.php';
        $project =  new Data($projectPath);
        $project->name = $name;
        $project->title = $title;
        $project->icon = 'icon.png';
        
        
        
        $folderList = ['controller','library','model','template'];
            
        foreach ($folderList as $folder) {
            $folderPath = $appPath.DS.$folder;
            if(!mkdir($folderPath)){
                throw new \Exception('创建应用失败');
            }
        }    

        //icon
        copy($this->app->getExamplePath().DS.'icon.png', $appPath.DS.'icon.png');

        //App.php
        file_put_contents($appPath.DS.'App.php', $this->app->getExpContent($name,'App'));

        //designer
        mkdir($appPath.DS.'_setup');

        switch($type){
            case 'front-admin':
                file_put_contents($appPath.DS.'controller'.DS.'index.php', $this->app->getExpContent($name,'Controller',['controller'=>'Index']));

                mkdir($appPath.DS.'controller'.DS.'admin');
                file_put_contents($appPath.DS.'controller'.DS.'admin'.DS.'index.php', $this->app->getExpContent($name,'AdminControllerDeep',['folder'=>'admin','controller'=>'Index']));
                $project->admin = [
                    'controller' => 'admin.index',
                    'action' =>'index'
                ];
            break;

            case 'front':
                file_put_contents($appPath.DS.'controller'.DS.'index.php', $this->app->getExpContent($name,'Controller',['controller'=>'Index']));
            break;

            case 'admin':
                file_put_contents($appPath.DS.'controller'.DS.'index.php', $this->app->getExpContent($name,'AdminController',['controller'=>'Index']));
                $project->admin = [
                    'controller' => 'index',
                    'action' =>'index'
                ];
            break;
        }

        $project->save();

        $this->log('创建应用',[
            'title' =>$title,
            'name' => $name,
            'type' => $type 
        ]);

        return true;
    }
 
    public function onSaveInfo(){
        $data['title'] =  $this->post('title',$this->check('标题')->type('string')->required());
        $data['icon'] =  $this->post('icon',$this->check('图标')->type('string')->required());
        $data['version'] =  $this->post('version',$this->check('版本')->type('string')->required());
        $data['description'] =  $this->post('description',$this->check('描述')->type('string'));
        $data['author'] =  $this->post('author',$this->check('作者')->type('string'));
        $data['copyright'] =  $this->post('copyright',$this->check('版权')->type('string'));
     

        $name = $this->post('name',$this->check('名称')->type('string')->required()->reg('/^[A-Za-z]{1}[A-Za-z0-9_]{2,49}$/'));



        if (!$this->app->design($name)){
            throw new \Exception('该应用不支持设计');
        }

        $projectPath =ROOT.DS.APP.DS.$name.DS.'project.php';
        $project =  new Data($projectPath);
        $project->read();
        foreach($data as $key=>$value){
            $project->$key =  $value;
        }
        
        $project->save();
        
        $driverList = $this->post('driverList',[]);

        $designerPath =ROOT.DS.APP.DS.$name.DS.'_setup'.DS.'config.php';
        $designer =  new Data($designerPath);
        $designer->read();
        $designer->driverList =  $driverList;
        $designer->save();
        
        
        return true;
    }
}